package com.mojito.learn.algorithm.interview;

import com.mojito.learn.algorithm.datastructure.TreeNode;

import java.util.LinkedList;

/**
 * @author liufq
 * @since 2022/4/18 下午6:26
 */
public class test {

    public static void main(String[] args) {
        TreeNode root = new TreeNode(5);
        TreeNode leftNode = new TreeNode(3);
        TreeNode leftNodeL = new TreeNode(2);
        TreeNode leftNodeR = new TreeNode(1);
        TreeNode rightNode = new TreeNode(6);
        TreeNode rightNodeL = new TreeNode(7);
        TreeNode rightNodeR = new TreeNode(8);
        leftNode.left = leftNodeL;
        leftNode.right = leftNodeR;
        rightNode.right = rightNodeR;
        rightNode.left = rightNodeL;
        root.left = leftNode;
        root.right = rightNode;
        LinkedList<Integer> linkedList = new LinkedList<>();
        linkedList.add(root.val);
        pathPrintln(root, linkedList);
    }

    public static void pathPrintln(TreeNode root, LinkedList<Integer> linkedList) {
        TreeNode left = root.left;
        if (left != null) {
            linkedList.add(left.val);
            pathPrintln(left, linkedList);
            linkedList.removeLast();
        }
        TreeNode right = root.right;
        if (right != null) {
            linkedList.add(right.val);
            pathPrintln(right, linkedList);
            linkedList.removeLast();
        }
        if (left == null && right == null) {
            System.out.println(linkedList);
        }
    }
}
